package com.ruoyi.web.controller.store;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.CommonResult;
import com.ruoyi.common.utils.HmStopUtil;
import com.ruoyi.store.bo.HmSystemAttachmentMoveBo;
import com.ruoyi.store.domain.HmSystemAttachment;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.store.vo.HmSystemAttachmentVo;
import com.ruoyi.store.bo.HmSystemAttachmentQueryBo;
import com.ruoyi.store.bo.HmSystemAttachmentAddBo;
import com.ruoyi.store.bo.HmSystemAttachmentEditBo;
import com.ruoyi.store.service.IHmSystemAttachmentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 素材管理Controller
 *
 * @author ruoyi
 * @date 2021-07-04
 */
@Api(value = "素材管理控制器", tags = {"素材管理管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/store/attachment")
public class HmSystemAttachmentController extends BaseController {

	private final IHmSystemAttachmentService iHmSystemAttachmentService;

	/**
	 * 查询素材管理列表
	 */
	@ApiOperation("查询素材管理列表")
	@PreAuthorize("@ss.hasPermi('store:attachment:list')")
	@GetMapping("/list")
	public TableDataInfo<HmSystemAttachmentVo> list(@Validated HmSystemAttachmentQueryBo bo) {
		return iHmSystemAttachmentService.queryPageList(bo);
	}

	/**
	 * 导出素材管理列表
	 */
	@ApiOperation("导出素材管理列表")
	@PreAuthorize("@ss.hasPermi('store:attachment:export')")
	@Log(title = "素材管理", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult<HmSystemAttachmentVo> export(@Validated HmSystemAttachmentQueryBo bo) {
		List<HmSystemAttachmentVo> list = iHmSystemAttachmentService.queryList(bo);
		ExcelUtil<HmSystemAttachmentVo> util = new ExcelUtil<HmSystemAttachmentVo>(HmSystemAttachmentVo.class);
		return util.exportExcel(list, "素材管理");
	}

	/**
	 * 获取素材管理详细信息
	 */
	@ApiOperation("获取素材管理详细信息")
	@PreAuthorize("@ss.hasPermi('store:attachment:query')")
	@GetMapping("/{attId}")
	public AjaxResult<HmSystemAttachmentVo> getInfo(@NotNull(message = "主键不能为空")
													@PathVariable("attId") Integer attId) {
		return AjaxResult.success(iHmSystemAttachmentService.queryById(attId));
	}

	/**
	 * 新增素材管理
	 */
	@ApiOperation("新增素材管理")
	@PreAuthorize("@ss.hasPermi('store:attachment:add')")
	@Log(title = "素材管理", businessType = BusinessType.INSERT)
	@RepeatSubmit
	@PostMapping()
	public AjaxResult<Void> add(@Validated @RequestBody HmSystemAttachmentAddBo bo) {
		return toAjax(iHmSystemAttachmentService.insertByAddBo(bo) ? 1 : 0);
	}

	/**
	 * 修改素材管理
	 */
	@ApiOperation("修改素材管理")
	@PreAuthorize("@ss.hasPermi('store:attachment:edit')")
	@Log(title = "素材管理", businessType = BusinessType.UPDATE)
	@RepeatSubmit
	@PutMapping()
	public AjaxResult<Void> edit(@Validated @RequestBody HmSystemAttachmentEditBo bo) {
		return toAjax(iHmSystemAttachmentService.updateByEditBo(bo) ? 1 : 0);
	}

	/**
	 * 删除素材管理
	 */
	@ApiOperation("删除素材管理")
	@PreAuthorize("@ss.hasPermi('store:attachment:remove')")
	@Log(title = "素材管理" , businessType = BusinessType.DELETE)
	@DeleteMapping("/{attIds}")
	public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
								   @PathVariable Integer[] attIds) {
		return toAjax(iHmSystemAttachmentService.deleteWithValidByIds(Arrays.asList(attIds), true) ? 1 : 0);
	}

	/**
	 * 查询附件管理表信息
	 * @param move SystemAttachmentMoveRequest
	 * @author Mr.Zhang
	 * @since 2020-06-02
	 */
	@ApiOperation(value = "更改图片目录")
	@RequestMapping(value = "/move", method = RequestMethod.POST)
	public CommonResult<String> updateAttrId(@RequestBody @Validated HmSystemAttachmentMoveBo move){
		LambdaUpdateWrapper<HmSystemAttachment> lup = new LambdaUpdateWrapper<>();
		lup.in(HmSystemAttachment::getAttId, HmStopUtil.stringToArray(move.getAttrId()));
		lup.set(HmSystemAttachment::getPid, move.getPid());
		if(iHmSystemAttachmentService.update(lup)){
			return CommonResult.success();
		}else{
			return CommonResult.failed();
		}
	}
	/**
	 * 删除附件管理表
	 * @param ids String
	 * @author Mr.Zhang
	 * @since 2020-06-02
	 */
	@ApiOperation(value = "删除")
	@RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
	public CommonResult<String> delete(@PathVariable String ids){
		if(iHmSystemAttachmentService.removeByIds(HmStopUtil.stringToArray(ids))){
			return CommonResult.success();
		}else{
			return CommonResult.failed();
		}
	}
}
